﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class Email: IEmail
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public Email(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.Email GetEmail(System.Int32 emailID)
		{
			BEGDC.PreContract.DAL.Email email = BEGDC.PreContract.DAL.Email.TryFind(emailID);

            return email;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetEmail_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM Email";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteEmail(System.Int32 emailID)
		{
			try
            {
				BEGDC.PreContract.DAL.Email email = BEGDC.PreContract.DAL.Email.TryFind(emailID);
                email.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Email", string.Format("Error occured when deleting the Email, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddEmail(System.String className, Nullables.NullableInt32 preContractID, System.String emailContent, System.String cC, System.String subject, System.Boolean isSend, Nullables.NullableDateTime sendTime)	
		{
			try
            {
				BEGDC.PreContract.DAL.Email email = new BEGDC.PreContract.DAL.Email();
				
				email.ClassName = className;
				email.PreContractID = preContractID;
				email.EmailContent = emailContent;
				email.CC = cC;
				email.Subject = subject;
				email.IsSend = isSend;
				email.SendTime = sendTime;
				email.CreateAndFlush();
				
				return email.EmailID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Email", string.Format("Error occured when adding a new Email, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateEmail(System.Int32 emailID, System.String className, Nullables.NullableInt32 preContractID, System.String emailContent, System.String cC, System.String subject, System.Boolean isSend, Nullables.NullableDateTime sendTime)
		{
			try
            {
                BEGDC.PreContract.DAL.Email email = GetEmail(emailID);

				email.ClassName = className;
				email.PreContractID = preContractID;
				email.EmailContent = emailContent;
				email.CC = cC;
				email.Subject = subject;
				email.IsSend = isSend;
				email.SendTime = sendTime;
				email.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Email", string.Format("Error occured when updating the Email, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



